package com.example.fw;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import com.example.tests.ContactObject;
import com.example.tests.GroupObject;

public class AppManager {

	public static WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();

	public AppManager() {
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	public void stop() {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
			
			public void createGroup(GroupObject groupObject) {
				openMainPage();
				goToGroupListPage();
				initGroupCreation();
				fillGroupForm(groupObject);
				submitFilledFormCreation();
				returnToGroupListPage();
			}

			public void createContact(ContactObject addContact) {
				openMainPage();
				fillContactForm(addContact);
				selectContactListBoxElement(addContact);
				submitFilledFormCreation();
				returnToContactListPage();
			}

			private void returnToGroupListPage() {
				driver.findElement(By.linkText("group page")).click();
			}

			private void returnToContactListPage() {
				driver.findElement(By.linkText("home page")).click();
			}

			public void submitFilledFormCreation() {
				driver.findElement(By.name("submit")).click();
			}

			private void fillGroupForm(GroupObject groupObject) {
				type(By.name("group_name"), groupObject.name);
				type(By.name("group_header"), groupObject.header);
				type(By.name("group_footer"), groupObject.footer);
			}

			public void fillContactForm(ContactObject contactObject) {
				type(By.name("firstname"), contactObject.firstname);
				type(By.name("lastname"), contactObject.lastname);
				type(By.name("address"), contactObject.address);
				type(By.name("home"), contactObject.home);
				type(By.name("work"), contactObject.work);
				type(By.name("mobile"), contactObject.mobile);
				type(By.name("email"), contactObject.email);
				type(By.name("email2"), contactObject.email2);
				type(By.name("byear"), contactObject.byear);
				type(By.name("address2"), contactObject.address2);
				type(By.name("phone2"), contactObject.phone2);
			}

			private void type(By locator, String paramvalue) {
				driver.findElement(locator).clear();
				driver.findElement(locator).sendKeys(paramvalue);
			}

			public void selectContactListBoxElement(ContactObject contactObject) {
				typeselect(By.name("bday"), contactObject.bday);
				typeselect(By.name("bmonth"), contactObject.bmonth);
				typeselect(By.name("new_group"), contactObject.newgroup);
			}

			private void typeselect(By locator, String optionvalue) {
				new Select(driver.findElement(locator))
						.selectByVisibleText(optionvalue);
			}

			public void goToContactCreation() {
				driver.findElement(By.linkText("add new")).click();
			}

			private void initGroupCreation() {
				driver.findElement(By.name("new")).click();
			}

			private void goToGroupListPage() {
				driver.findElement(By.linkText("groups")).click();
			}

			public void openMainPage() {
				driver.get("http://localhost/addressbookv4.1.4/");
			}

		
	}

